
package com.ifcar.app.activity.tab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ifcar.app.activity.NewsPage;
import com.ifcar.app.activity.SpecialAritcleTwo;
import com.ifcar.app.activity.newspage.NewsFragment;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.fragment.NRAbsPullLoadingListFrament;
import com.ifcar.app.model.News;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.BaseImageDownloader;
import com.ifcar.app.util.BasePageViewAdapter;
import com.ifcar.app.util.ModelUtils;
import com.ifcar.app.util.PicUtil;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.util.Tools;
import com.ifcar.app.view.LoadingListView;
import com.ifcar.app.activity.R;


public class HomeListFragment extends NRAbsPullLoadingListFrament implements
        OnItemClickListener {
    public static final int ERROR_TYPE_NO_COLUMN_ID = 1;

    public static final String COLUMN_ID = "columnId";

    public static final String COLUMN_URL = "columnURL";

    public static final String COLUMN_NAME = "columnName";
    
    public static final String HEADER = "header";

    public static final String POSITION = "position";

    String mColumnID;

    String mColumnURL;

    String mColumnName;
    
    boolean mHasHeader;

    List<Map<String, Object>> mNewsList;
    
    List<Map<String, Object>> mNewsAdapterList = new ArrayList<Map<String,Object>>();

    BasePageViewAdapter mAdapter;

    BaseImageDownloader mImageDownloader;
    
    private View mHeaderLay;
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mColumnID = bundle.getString(COLUMN_ID);
            mColumnURL = bundle.getString(COLUMN_URL);
            mColumnName = bundle.getString(COLUMN_NAME);
            mHasHeader = bundle.getBoolean(HEADER);
        }else{
            mColumnID = "T1295505916992";
            mColumnURL = "T1295505916992";
            mColumnName = "体育";
        }

        setRefreshTag(mColumnID);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        mImageDownloader = new BaseImageDownloader(getActivity(), null);
        mImageDownloader.setMode(BaseImageDownloader.Mode.CORRECT);

        BitmapDrawable defaultNewsDrawable = (BitmapDrawable) getThemeSettingsHelper()
                .getThemeDrawable(getActivity(), R.drawable.default_news);
        mImageDownloader.setDefaultBitmap(defaultNewsDrawable.getBitmap());
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }  

    @Override
	public ListAdapter getAdapter() {
		return mAdapter;
	}

    @Override
	public void onLoadingListViewAttached(LoadingListView loadingListView) {
        if (mHasHeader) {
            if (mHeaderLay == null) {
                LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
                mHeaderLay = layoutInflater.inflate(R.layout.top_image_text, null);
                mHeaderLay.findViewById(R.id.top_tag).setVisibility(View.GONE);
            }

            loadingListView.getListView().addHeaderView(mHeaderLay);
        }
        
		super.onLoadingListViewAttached(loadingListView);
		if (mAdapter != null && mAdapter.getCount() > 0) {
            callLoadingListViewAddMoreView();
        }
        loadingListView.getListView().setOnItemClickListener(this);
	}

    @Override
    public void onLoadingListViewDetached(LoadingListView loadingListView) {
        super.onLoadingListViewDetached(loadingListView);
        if (mHasHeader && mHeaderLay != null) {
            loadingListView.getListView().removeHeaderView(mHeaderLay);
        }
        
    }

    @Override
    public void onTabSelected(boolean first) {
        super.onTabSelected(first);
        if (first) {
            startLoading();
        }

        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onTabUnSelected() {
        super.onTabUnSelected();
        ListView listView = getListView();
        if (listView != null) {
            final int count = listView.getChildCount();
            for (int i = 0; i < count; i++) {
                ImageView img = (ImageView) listView.getChildAt(i).findViewById(R.id.img);
                if (img != null) {
                    img.setImageDrawable(null);
                }
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        boolean isHeadline = false;
        String docid = "";
        String url = "";
        String imgsrc = "";
        String digest = "";
        String replyCount = "";
        String title = "";
        String ptime = "";
        String sdocid = "";// 专题ID

        Context context = getActivity();
        String columnName = mColumnName;
        String columnID = mColumnID;

//        Intent itemintent = new Intent(context, DetailPage.class);
        Intent itemintent = new Intent(context, NewsPage.class);
        Bundle b = new Bundle();
        
        String idJsonString = StringUtil.createIdString(mNewsAdapterList);
        if(!TextUtils.isEmpty(idJsonString)){
        	b.putString(NewsFragment.ID_JSON_STRING, idJsonString);
        }
        
        try {
            b.putString(GetColumnsInfo.TNAME_TAG, columnName);
            if (isHeadline) {
                if (position == 0) {
                    // docid =
                    // first.get(GetColumnsInfo.DOCID_TAG)==null?"":first.get(GetColumnsInfo.DOCID_TAG).toString();
                    // url =
                    // first.get(GetColumnsInfo.URL_TAG)==null?"":first.get(GetColumnsInfo.URL_TAG).toString();
                    // imgsrc =
                    // first.get(GetColumnsInfo.IMGSRC_TAG)==null?"":first.get(GetColumnsInfo.IMGSRC_TAG).toString();
                    // digest =
                    // first.get(GetColumnsInfo.DIGEST_TAG)==null?"":first.get(GetColumnsInfo.DIGEST_TAG).toString();
                    // replyCount =
                    // first.get(GetColumnsInfo.REPLYCOUNT_TAG)==null?"":first.get(GetColumnsInfo.REPLYCOUNT_TAG).toString();
                    // title =
                    // first.get(GetColumnsInfo.TITLE_TAG)==null?"":first.get(GetColumnsInfo.TITLE_TAG).toString();
                    // ptime =
                    // first.get(GetColumnsInfo.PTIME_TAG)==null?"":first.get(GetColumnsInfo.PTIME_TAG).toString();
                    // sdocid =
                    // first.get(GetColumnsInfo.SPECIAL_ID_TAG)==null?"":first.get(GetColumnsInfo.SPECIAL_ID_TAG).toString();
                    return;
                } else {
                    // docid = data.get(position -
                    // 1).get(GetColumnsInfo.DOCID_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.DOCID_TAG).toString();
                    // url = data.get(position -
                    // 1).get(GetColumnsInfo.URL_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.URL_TAG).toString();
                    // imgsrc = data.get(position -
                    // 1).get(GetColumnsInfo.IMGSRC_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.IMGSRC_TAG).toString();
                    // digest = data.get(position -
                    // 1).get(GetColumnsInfo.DIGEST_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.DIGEST_TAG).toString();
                    // replyCount = data.get(position -
                    // 1).get(GetColumnsInfo.REPLYCOUNT_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.REPLYCOUNT_TAG).toString();
                    // title = data.get(position -
                    // 1).get(GetColumnsInfo.TITLE_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.TITLE_TAG).toString();
                    // ptime = data.get(position -
                    // 1).get(GetColumnsInfo.PTIME_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.PTIME_TAG).toString();
                    // sdocid = data.get(position -
                    // 1).get(GetColumnsInfo.SPECIAL_ID_TAG)==null?"":data.get(position
                    // - 1).get(GetColumnsInfo.SPECIAL_ID_TAG).toString();
                }
            } else {
                // List<HashMap<String, Object>> listmap = (List<HashMap<String,
                // Object>>)dataMap4NonHead.get(columnID).getData();
                List<Map<String, Object>> listmap = mNewsList;
                if (listmap != null) {
                    docid = listmap.get(position).get(GetColumnsInfo.DOCID_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.DOCID_TAG));
                    url = listmap.get(position).get(GetColumnsInfo.URL_TAG) == null ? "" : StringUtil.GetString(listmap
                            .get(position).get(GetColumnsInfo.URL_TAG));
                    imgsrc = listmap.get(position).get(GetColumnsInfo.IMGSRC_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.IMGSRC_TAG));
                    digest = listmap.get(position).get(GetColumnsInfo.DIGEST_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.DIGEST_TAG));
                    replyCount = listmap.get(position).get(GetColumnsInfo.REPLYCOUNT_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.REPLYCOUNT_TAG));
                    title = listmap.get(position).get(GetColumnsInfo.TITLE_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.TITLE_TAG));
                    ptime = listmap.get(position).get(GetColumnsInfo.PTIME_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.PTIME_TAG));
                    sdocid = listmap.get(position).get(GetColumnsInfo.SPECIAL_ID_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.SPECIAL_ID_TAG));
                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Log.i("Newsreader", "---------------------sdocid:"+sdocid);
        // 测试使用
        // sdocid = "S1310611586167";

        if (docid != null && !docid.equals("")) {
            // 标识为已读
            // setIsReaded4List(position, docid);

            b.putString(GetColumnsInfo.DOCID_TAG, docid);
            b.putString(GetColumnsInfo.URL_TAG, url);
            b.putString(GetColumnsInfo.IMGSRC_TAG, imgsrc);
            b.putString(GetColumnsInfo.DIGEST_TAG, digest);
            b.putString(GetColumnsInfo.REPLYCOUNT_TAG, replyCount);
            b.putString(GetColumnsInfo.TITLE_TAG, title);
            b.putString(GetColumnsInfo.PTIME_TAG, ptime);
            b.putString(GetColumnsInfo.SPECIAL_ID_TAG, sdocid);
            b.putString(GetColumnsInfo.TID_TAG, columnID);
            b.putString(POSITION, "" + position);
            b.putBoolean(GetColumnsInfo.HEADLINE_TAG, isHeadline);

            if (null != sdocid && !"".equals(sdocid)) {
                itemintent = new Intent(context, SpecialAritcleTwo.class);
            }
            itemintent.putExtras(b);
            startActivity(itemintent);
        } else if (null != sdocid && !"".equals(sdocid)) {
            itemintent = new Intent(context, SpecialAritcleTwo.class);
            b.putString(GetColumnsInfo.DOCID_TAG, docid);
            b.putString(GetColumnsInfo.URL_TAG, url);
            b.putString(GetColumnsInfo.IMGSRC_TAG, imgsrc);
            b.putString(GetColumnsInfo.DIGEST_TAG, digest);
            b.putString(GetColumnsInfo.REPLYCOUNT_TAG, replyCount);
            b.putString(GetColumnsInfo.TITLE_TAG, title);
            b.putString(GetColumnsInfo.PTIME_TAG, ptime);
            b.putString(GetColumnsInfo.SPECIAL_ID_TAG, sdocid);
            b.putString(GetColumnsInfo.TID_TAG, columnID);
            b.putString(POSITION, "" + position);
            b.putBoolean(GetColumnsInfo.HEADLINE_TAG, isHeadline);
            itemintent.putExtras(b);
            startActivity(itemintent);
        } else {
            Toast.makeText(context, "打开新闻正文失败", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected int canLoading() {
        if (TextUtils.isEmpty(mColumnID)) {
            return ERROR_TYPE_NO_COLUMN_ID;
        }
        return super.canLoading();
    }

    @Override
    protected void onLoading() {
        super.onLoading();
        loadingNews();
    }

    void loadingNews() {
        // 加载本地数据,没有启用新线程
        List<Map<String, Object>> list = News.getListFromDB(getDb(), mColumnID, "nonhead");
        Message msg = getHandler().obtainMessage(MSG_LOADING);
        msg.obj = list;
        getHandler().dispatchMessage(msg);
    }

    @Override
    protected void onHandleLoading(Object result) {
        super.onHandleLoading(result);
        List<Map<String, Object>> list = (List<Map<String, Object>>) result;       
        
        if (list != null && list.size() > 0) {
            callLoadingListViewAddMoreView();
        }
 
        setData(list);

        // 当本地有数据的情况下延时200ms再刷新,如果马上刷新在网速较快的情况下可能造成tab界面切换时卡
        if (mAdapter != null && mAdapter.getCount() > 0) {
            if (isNeedRefresh()) {
                getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refresh();
                    }
                }, 200);
            }
        } else {
            refresh();
        }
        
        
    }

    
    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
        
    }

    @Override
    protected int canRefreshing() {
        if (TextUtils.isEmpty(mColumnID)) {
            return ERROR_TYPE_NO_COLUMN_ID;
        }
        return super.canRefreshing();
    }

    @Override
    protected void onRefreshing() {
        super.onRefreshing();
        callLoadingListViewStartLoading(mAdapter == null);
        refreshingNews();
    }

    void refreshingNews() {
        final Context context = getActivity();
        final SQLiteDatabase sqldb = getDb();
        final String id = mColumnID;
        final String columnUrl = mColumnURL;
        final boolean hasHeader = mHasHeader;
        new Thread(new Runnable() {
            @Override
            public void run() {

                List<Map<String, Object>> tmpList = GetColumnsInfo.getArticleList(
                        getUrlPrefix(), columnUrl, false, hasHeader, 0, getLoadingNum(), context);

                if (tmpList != null && tmpList.size() > 0) {
                    sqldb.execSQL(
                            "delete from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE
                                    + " where news_col_id='" + id + "'");
                    News.insertListToDB(sqldb, tmpList, id, "nonhead");
                }
                Message msg = getHandler().obtainMessage(MSG_REFRESHING);
                msg.obj = tmpList;
                getHandler().sendMessage(msg);
            }
        }).start();
    }
    
    protected String getUrlPrefix() {
        return GetColumnsInfo.NonHeadline_list_Url;
    }

    @Override
    protected void onHandleRefreshing(Object result) {
        super.onHandleRefreshing(result);
        final Context context = getActivity();
        if(null!=context){
            StringUtil.clearOfflineDownloadRefreshTime(context);
        }
        List<Map<String, Object>> data = (List<Map<String, Object>>) result;
        
        if (data != null && data.size() > 0) {
            callLoadingListViewAddMoreView();
        } else if (mAdapter == null || mAdapter.getCount() == 0) {
            callLoadingListViewRemoveMoreView();
        }

        if (data != null && data.size() != 0) {
        	callLoadingListViewCompleteLoading(true, true);
            setData(data);
        } else {
            Toast.makeText(getActivity(), R.string.msg_loading_failed, Toast.LENGTH_SHORT).show();
            callLoadingListViewCompleteLoading(false, true);
        }
    }

    @Override
    protected int canLoadingMore() {
        if (TextUtils.isEmpty(COLUMN_ID)) {
            return ERROR_TYPE_NO_COLUMN_ID;
        }
        return super.canLoadingMore();
    }

    @Override
    protected void onLoadingMore() {
        super.onLoadingMore();
        final int num = getLoadingNum();
        final int start = (mNewsList == null || mNewsList.size() == 0) ? 0 : getLoadingStart() + num;
        callLoadingListViewStartLoadingMore(start);
        loadingMoreNews(start, num);
    }

    void loadingMoreNews(final int start, final int num) {
        final Context context = getActivity();
        // 取网络
        new Thread(new Runnable() {
            @Override
            public void run() {

                List<Map<String, Object>> tmpList = GetColumnsInfo.getArticleList(getUrlPrefix(),
                        mColumnURL, start, num, context);

                if (tmpList != null) {
                    synchronized (HomeListFragment.this) {
                        if (mNewsList != null) {
                            removeRepeatedData(mNewsList, tmpList, GetColumnsInfo.DOCID_TAG);
                        }
                    }
                }

                Message msg = getHandler().obtainMessage(MSG_LOADING_MORE);
                msg.obj = tmpList;
                getHandler().sendMessage(msg);
            }
        }).start();
    }

    @Override
    protected void onHandleLoadingMore(Object result) {
        super.onHandleLoadingMore(result);
        List<Map<String, Object>> data = (List<Map<String, Object>>) result;
        if (data == null) {
            Toast.makeText(getActivity(), R.string.msg_loading_failed, Toast.LENGTH_SHORT).show();
            callLoadingListViewCompleteLoadingMore(false, true);
        } else if (data.size() == 0) {
            Toast.makeText(getActivity(), R.string.msg_loading_nomore, Toast.LENGTH_SHORT).show();
            callLoadingListViewCompleteLoadingMore(true, false);
        } else {
            callLoadingListViewCompleteLoadingMore(true, true);
            addData(data);
        }
    }

    void setData(List<Map<String, Object>> data) {
        if (data != null && data.size() > 0) {
            synchronized (this) {
                cacheDocidListMap(StringUtil.NONHEAD_TYPE, "0", data, mColumnID);
                mNewsList = data;
                mNewsAdapterList.clear();
                mNewsAdapterList.addAll(data);
                if (mHasHeader) {
                    Map<String, Object> first = mNewsAdapterList.remove(0);
                    addHeader(first);
                }

                String imgshow = "0";
                if (null != data && data.size() > 0) {
                    Map<String, Object> selectormap = data.get(0);
                    imgshow = (String) selectormap.get(GetColumnsInfo.HASIMG_TAG);
                }

                if ("1".equals(imgshow)) {
                    if(mColumnID.equals(StringUtil.COLUMN_TOPIC_BLOG)){//如果是博客栏目 来自于话题tab
                        mAdapter = new BasePageViewAdapter(getActivity(), mNewsAdapterList,
                                R.layout.list_item_img_blog, new String[] {
                                        GetColumnsInfo.TITLE_TAG, GetColumnsInfo.SUBTITLE_TAG,
                                        GetColumnsInfo.PTIME_TAG, GetColumnsInfo.IMGSRC_TAG
                                }, new int[] {
                                        R.id.title, R.id.subtitle, R.id.article_date, R.id.img
                                }, mImageDownloader);
                        mAdapter.setFormatTime(false);
                    }else if(mColumnID.equals(StringUtil.COLUMN_TOPIC_DUJIA)){//如果是独家解读栏目 来自于话题tab
                        mAdapter = new BasePageViewAdapter(getActivity(), mNewsAdapterList,
                                R.layout.list_item_img_nohead, new String[] {
                                        GetColumnsInfo.TITLE_TAG, GetColumnsInfo.DIGEST_TAG,
                                        GetColumnsInfo.SUBTITLE_TAG, GetColumnsInfo.IMGSRC_TAG
                                }, new int[] {
                                        R.id.title, R.id.desc_title, R.id.comment_count, R.id.img
                                }, mImageDownloader);
                    }else if(Tools.isColumnWithImgFromNews(mColumnID)){//来自与新闻tab下的栏目且需要显示图片               
                        mAdapter = new BasePageViewAdapter(getActivity(), mNewsAdapterList,
                                R.layout.list_item_img_nohead, new String[] {
                                        GetColumnsInfo.TITLE_TAG, GetColumnsInfo.DIGEST_TAG,
                                        GetColumnsInfo.REPLYCOUNT_TAG, GetColumnsInfo.IMGSRC_TAG
                                }, new int[] {
                                        R.id.title, R.id.desc_title, R.id.comment_count, R.id.img
                                }, mImageDownloader);
                    }else{//其他栏目
                        mAdapter = new BasePageViewAdapter(getActivity(), mNewsAdapterList,
                                R.layout.list_item_img_nohead, new String[] {
                                        GetColumnsInfo.TITLE_TAG, GetColumnsInfo.DIGEST_TAG,
                                        GetColumnsInfo.IMGSRC_TAG
                                }, new int[] {
                                        R.id.title, R.id.desc_title, R.id.img
                                }, mImageDownloader);
                    }
                    
                } else {
                    mAdapter = new BasePageViewAdapter(getActivity(), mNewsAdapterList,
                            R.layout.list_item_nohead, new String[] {
                                    GetColumnsInfo.TITLE_TAG, GetColumnsInfo.REPLYCOUNT_TAG,
                                    GetColumnsInfo.PTIME_TAG
                            }, new int[] {
                                    R.id.title, R.id.comment_count, R.id.article_date
                            }, mImageDownloader);
                }
                float density = getResources().getDisplayMetrics().density;
                mImageDownloader.setBitmapWandH(((int)(73 * density)), ((int)(53 * density)), "Y");
                mAdapter.setDB(getDb());
                mAdapter.setAdapterType("news");
                ListView listView = getListView();
                if (listView != null) {
                    listView.setAdapter(mAdapter);
                }
            }

        }
    }

    void addData(List<Map<String, Object>> data) {
        if (data == null || data.size() == 0 || mNewsList == null) {
            return;
        }
        mNewsList.addAll(data);
        mNewsAdapterList.addAll(data);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }
    
    void addHeader(Map<String, Object> first) {
        if (mHeaderLay == null) {
            return;
        }
        
        ImageView image = (ImageView) mHeaderLay.findViewById(R.id.top_image);
        TextView name = (TextView) mHeaderLay.findViewById(R.id.top_text);
        name.setText((String)ModelUtils.getValue(first, GetColumnsInfo.TITLE_TAG));
        
        HashMap<String, Integer> whmap = Tools.getWidth_Height(getActivity());
        int screenwidth = whmap.get("w");

        int w = screenwidth;
        int h = Tools.getCutHeadPicHeight(screenwidth);
        BaseImageDownloader imageDownloader = new BaseImageDownloader(getActivity(),
                ((BitmapDrawable) getResources().getDrawable(R.drawable.vote_default))
                        .getBitmap());
        imageDownloader.setThread(Thread.currentThread());
        image.getLayoutParams().height = h;
        String imgsrc = PicUtil.get3gImgurl(
                (String) ModelUtils.getValue(first, GetColumnsInfo.IMGSRC_TAG), w, h);
        imageDownloader.download(imgsrc, image);
    }

    void cacheDocidListMap(int type, String key, List<?> data, String columnID) {
        ArrayList<String> docIDList = new ArrayList<String>();
        for (int k = 0; k < data.size(); k++) {
            if (type == StringUtil.TOPIC_TYPE)
                docIDList.add(StringUtil.GetString(((Map<String, Object>) data.get(k)).get(GetColumnsInfo.DOCID_TAG)));
            else
                docIDList.add(StringUtil.GetString(((Map<String, Object>) data.get(k)).get(GetColumnsInfo.DOCID_TAG)));
        }
        getApplication().setDocIdListMap(type, key, docIDList, columnID);
    }

    void removeRepeatedData(List<Map<String, Object>> sourceData,
            List<Map<String, Object>> data, String key) {
        String docid = "";
        
        if(null!=data){
            
            for (int i = 0; i < data.size(); i++) {
                if (((HashMap<String, Object>) data.get(i)).get(key) != null) {
                    docid = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(key));
                    for (int j = 0; j < sourceData.size(); j++) {
                        if (((HashMap<String, Object>) sourceData.get(j)).get(key) != null
                                && docid.equals(StringUtil.GetString(((HashMap<String, Object>) sourceData.get(j)).get(key)))) {
                            synchronized (data) {
                                data.remove(i);
                            }
                            i = i - 1;
                            j = sourceData.size();
                        }
                    }
                }
            }
        }
        
    }

    public String getColumnID() {
        return mColumnID;
    }

    public String getColumnURL() {
        return mColumnURL;
    }


    @Override
    public boolean isNeedRefresh() {
        // TODO Auto-generated method stub
        boolean needRefresh = super.isNeedRefresh();
        if(!needRefresh){
            return needRefresh;
        }else{
            return StringUtil.isTipsAfterOfflineDownload(getActivity());
        }
        
    }
    
    @Override
    public void onApplyTheme() {
        super.onApplyTheme();
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        Context context = getActivity();
        
        if (mHeaderLay != null) {
            themeSettingsHelper.setTextViewColor(context,
                    (TextView) mHeaderLay.findViewById(R.id.top_text), R.color.top_text_color);
            themeSettingsHelper.setViewBackgroud(context,
                    mHeaderLay.findViewById(R.id.top_bottom_space), R.drawable.top_text_background);
        }

        BitmapDrawable defaultNewsDrawable = (BitmapDrawable) themeSettingsHelper.getThemeDrawable(
                context, R.drawable.default_news);
        mImageDownloader.setDefaultBitmap(defaultNewsDrawable.getBitmap());

        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }
    
    private class UpdateReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(StringUtil.OFFLINEUPDATE.equals(action)){
                startLoading();
            }
        }
    }

}
